<body>
    <!-- 列表头高级查询 -->
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>高级查询</legend>
    </fieldset>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">菜谱编号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="menuId" autocomplete="off" class="layui-input" placeholder="请输入">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">菜谱名称:</label>
                <div class="layui-input-inline">
                    <input type="text" name="menuName" autocomplete="off" class="layui-input" placeholder="请输入">
                </div>
            </div>
        </div>

        <div class="layui-form-item" style="text-align: center">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="menuDoSearch">查询</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="doReset">重置</button>
            </div>
        </div>
    </form>

    <div class="layui-btn-group demoTable">
        <button class="layui-btn" data-type="getCheckData" id="menuAlldata">获取选中行数据</button>
        <button class="layui-btn" data-type="getCheckLength" id="menuAllNum">获取选中数目</button>
        <button class="layui-btn" data-type="refresh" id="menuAllRefresh">刷新</button>
        <button class="layui-btn" data-type="addUser" id="menuAllAdd">添加</button>
    </div>

    <table class="layui-hide" id="menuTables" lay-filter="menuTables"></table>

    <!-- 按钮 -->
    <div style="display: none" id="menubarDemo" type="text/html">
        <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="detail">查看</a>
        <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
    </div>


    <script>

        layui.use(['jquery', 'layer', 'table', 'form', 'config'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var table = layui.table;
            var form = layui.form;
            var config = layui.config;
            // 表格渲染
            table.render({
                elem: '#menuTables'
                , url: config.base_server + 'getMenuList'
                , id: "menudemo"
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , cols: [[
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'menuId', width: 80, title: '菜谱编号', sort: true, width: '8%', minWidth: 100 }
                    , { field: 'menuName', width: 80, title: '菜谱名称', width: '10%', minWidth: 100 }
                    , {
                        field: 'menuImg', width: 80, title: '菜谱图片', width: '15%', minWidth: 100,
                        templet: function (d) { return '<div ><img src=' + d.menuImg + ' style="width: 50px;"></div>' }
                    }
                    , { field: 'menuTag', width: 80, title: '菜谱标签', width: '8%', minWidth: 100 }
                    , { field: 'menuComm', width: 80, title: '菜谱简介', width: '8%', minWidth: 100 }
                    , { field: 'menuTime', width: 80, title: '菜谱时区', width: '8%', minWidth: 100 }
                    , { field: 'menuPrice', width: 80, title: '就餐人数', width: '8%', minWidth: 100 }
                    , { field: 'menuType', width: 80, title: '菜谱类型', width: '10%', minWidth: 100 }
                    , { field: 'menuHot', width: 80, title: '菜谱热量', width: '8%', minWidth: 100 }
                    , { title: '操作', toolbar: '#menubarDemo' }


                ]]
                , page: true //开启分页
                , skin: 'row' //行边框风格
                , even: true //开启隔行背景
                , size: 'lg' //小尺寸的表格
            });
            //行内按钮事件的监听
            table.on('tool(menuTables)', function (obj) {
                var data = obj.data;
                if (obj.event === 'detail') {
                    var editId = data.menuId;
                    var fu = layer.open({
                        type: 2,
                        title: "查看" + editId + "号菜谱",
                        area: ['600px', '800px'],
                        fix: false,
                        content: 'components/system/menuDetailTable.html?editNo=' + editId,
                        maxmin: true,
                        end: function () {
                            table.reload('menudemo');
                        }
                    });

                } else if (obj.event === 'del') {
                    var editId = data.menuId;
                    layer.confirm('真的删除该菜谱吗?', function (index) {
                        $.ajax({
                            type: "POST",
                            url: config.base_server + "deleteMenuById",
                            data: { menuId: editId },
                            dataType: "json",
                            success: (data) => {
                                if (data.code) {
                                    obj.del();
                                    layer.msg('删除成功', {
                                        icon: 1
                                    });
                                    layer.close(index);
                                } else {
                                    alert(data.info);
                                }
                            }
                        });
                    });
                } else if (obj.event === 'edit') {
                    var editId = data.menuId;
                    var fu = layer.open({
                        type: 2,
                        title: "编辑" + editId + "号菜谱",
                        fix: false,
                        area: ['1000px', '100vh'],
                        content: 'components/system/menuEditTable.html?editNo=' + editId,
                        maxmin: true,
                        end: function () {

                            table.reload('menudemo', {
                                url: config.base_server + 'getMenuList' //数据接口  
                                , where: {} //设定异步数据接口的额外参数        
                            });
                        }
                    });

                }
            });

            $('#menuAlldata').on('click', function () {
                var checkStatus = table.checkStatus('menudemo')
                    , data = checkStatus.data;
                layer.alert(data.map(item => {
                    return item.menuName;
                }).join(","));

            });
            $('#menuAllNum').on('click', function () {

                var checkStatus = table.checkStatus('menudemo')
                    , data = checkStatus.data;
                var datas = JSON.stringify(data)
                var ids = ''
                for (var i = 0; i < datas.length; i++) {
                    ids = ids + datas[i].UserNo
                }
                layer.msg('选中了：' + data.length + ' 个');

            });

            $('#menuAllRefresh').on('click', function () {
                location.reload();
            });

            //添加按钮 
            $('#menuAllAdd').on('click', function () {
                var fu = layer.open({
                    type: 2,
                    title: '添加',
                    maxmin: true,
                    area: ['1000px', '100vh'],
                    content: 'components/system/menuAddTable.html',
                    end: function () {
                        table.reload('menudemo');
                    }
                });
                layer.full(fu);
            });

            $('#btn5').on('click', function () {
                location.reload();
            });

            //  查询按钮
            $('#menuDoSearch').on('click', function () {
                var menuId = $("input[name='menuId']").val();
                var menuName = $("input[name='menuName']").val();
                table.reload('menudemo', {
                    url: config.base_server + 'getMenuList' //数据接口
                    , where: { menuId: menuId, menuName: menuName }
                });


            });

            //重置：待解决刷新问题 
            $('#doReset').on('click', function () {
                location.reload();
            });
        });
    </script>

</body>